package flink.api_study.source;

import org.apache.flink.streaming.api.functions.source.SourceFunction;

import java.sql.Timestamp;
import java.util.*;

/**
 * Created by tzs on 2022/8/2.
 */
public class PersonSource  implements SourceFunction<Person> {
    //用来控制数据产生
    private boolean running = true;

    private String[] jobs = {"程序员","产品经理","测试员"};
    private String[] sex = {"男","女"};

    @Override
    public void run(SourceContext<Person> sourceContext) throws Exception {
        Random r = new Random();
        while(running){
            Person p = new Person();
            p.setUuid(UUID.randomUUID().toString());
            p.setAge((int)(r.nextGaussian()*Math.sqrt(10)+28));//正态分布
            p.setJob(jobs[r.nextInt(3)]);//随机工作
//            p.setJob(jobs[0]);//固定工作
            p.setSex(sex[r.nextInt(2)]);//固定性别
            p.setTimestamp(new Timestamp(new Date().getTime()));
            sourceContext.collect(p);
            Thread.sleep(1000);//控制输出频率
        }
    }

    @Override
    public void cancel() {
        running = false;
    }
}
